#include "stm32f4xx.h"
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_tim.h>

#include <stdio.h>
#include <time.h>



void SysTick_Handler(void);


#define SYSTICK_FREQUENCY_HZ       1000

void InitializeLEDs();
void InitializeTimer();
void setup();
void nRF24L01_Delay_us(unsigned long n);


int main(void){
  printf("Hello ARM World!\n");
  unsigned long start, end;
  unsigned long res = 0;
  int seconds = 0;
  setup();
  /* Infinite loop */
  while (1){
	  start = 0;
	  end = 0;
	  res = 0;
	  /* 15 ms */
	  seconds = 10000;
	  start = TIM_GetCounter(TIM2);
	  while(seconds != 0){
		  nRF24L01_Delay_us(1);
		  seconds--;
	  }
	  end = TIM_GetCounter(TIM2);
	  res = end - start;
	  printf("Start: %lu!\n", start);
	  printf("End: %lu!\n", end);
	  printf("Risultato: %lu!\n", res);

  }
}

void setup(){
	InitializeTimer();
}

void nRF24L01_Delay_us(unsigned long n) {
	unsigned long i;

	while (n--)  // delay n us
	{
		i = 25;
		while (i--)
			; // delay 1 us
	}
}

void InitializeTimer(){
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseInitTypeDef timerInitStructure;
    timerInitStructure.TIM_Prescaler = 0;
    timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    timerInitStructure.TIM_Period = 0xffffffff;
    timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    timerInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM2, &timerInitStructure);
    TIM_Cmd(TIM2, ENABLE);
}


void SysTick_Handler(void){
}


